---
title: "Figure 3"
author: "Yingjie Fan"
date: "2023-06-02"
---

```{r setup, include=FALSE}
rm(list=ls())

#install.packages(c("dplyr", "tidyr", "ggplot2", "showtext", "likert", "data.table", "zoo", "ggrepel","Hmisc"))
library(dplyr)
library(tidyr)
library(ggplot2)
library(showtext)
library(likert)
library(data.table)
library(zoo)
library(ggrepel)
library(Hmisc)
path = "/Users/jpan/Downloads/CGTN_replication" # Insert path
```

```{r Load the data for Figure 3(a) &(b)}
content_d<-fread(paste0(path,'/Data/Classification/output_summary.csv'))%>%
  mutate(timestamp=as.Date(timestamp,"%Y-%m-%d"))%>%
  mutate(month=as.yearmon(substr(timestamp,1,7)))%>%
  mutate(D=ifelse(timestamp>"2017-07-20",1,0))

# Generate subset for Soft News
set.seed(123)
softnews<-content_d %>%
  select(username,month,mean_softnews) %>% 
    group_by(username,month) %>%
     summarise(data = list(smean.cl.boot(cur_data(), conf.int = .95, B = 1000, na.rm = TRUE))) %>%
  tidyr::unnest_wider(data)%>%
  mutate(D=ifelse(as.yearmon(month)>"2017-07-20",1,0))

# Generate subset for Narratives
set.seed(123)
narratives<-content_d %>%
  select(username,month,mean_narrative) %>% 
    group_by(username,month) %>%
     summarise(data = list(smean.cl.boot(cur_data(), conf.int = .95, B = 1000, na.rm = TRUE))) %>%
  tidyr::unnest_wider(data)%>%
  mutate(D=ifelse(as.yearmon(month)>"2017-07-20",1,0))
```

```{r Figure 3(a) Share of Tweets with Narratives}
narrative_p<-narratives%>%
ggplot(aes(x=month, y=Mean,linetype=username))+
  geom_line(aes(group=username))+
  geom_ribbon(aes(x=month, y=Mean, ymax=Lower, ymin=Upper), 
              alpha=0.2)+
     expand_limits(x=as.yearmon("Jul 2021"))+
   scale_x_yearmon(format = "%Y")+
    theme_bw()+
  ylim(0,0.8)+
  geom_text_repel(aes(label=ifelse(month=="December 2019",as.character(username),'')),hjust=-1.2,vjust=0.3,direction= "y",segment.color = 'grey80')+
  labs(y="Share of Tweets with Narratives", x = "",title="")+ 
  theme(legend.position = "none",
        legend.title = element_blank(),
        legend.text=element_text(size=12), axis.text.x = element_text(size=12),
        axis.text.y = element_text(size=12), axis.title=element_text(size=12))+
  scale_linetype_manual(values = c("CGTN" = "solid", "Xinhua" = "dotted","China Daily" = "longdash","People\'s Daily" = "dotdash")) 
ggsave(paste0(path,'/Output/figure3a_narrative.png'),narrative_p,width=7, height=5)
```

```{r Figure 3(b) Share of Soft News Tweets}
soft_p<-softnews%>%
ggplot(aes(x=month, y=Mean,linetype=username))+
  geom_line(aes(group=username))+
  geom_ribbon(aes(x=month, y=Mean, ymax=Lower, ymin=Upper), 
              alpha=0.2)+
     expand_limits(x=as.yearmon("Jul 2021"))+
   scale_x_yearmon(format = "%Y")+
  ylim(0,0.6)+
  theme_bw()+
  geom_text_repel(aes(label=ifelse(month=="December 2019",as.character(username),'')),hjust=-1.2,vjust=0.3,direction= "y",segment.color = 'grey80')+
  labs(y="Share of Soft News", x = "",title="")+ 
  theme(legend.position = "none",
        legend.title = element_blank(),
        legend.text=element_text(size=12), axis.text.x = element_text(size=12),
        axis.text.y = element_text(size=12), axis.title=element_text(size=12))+
  scale_linetype_manual(values = c("CGTN" = "solid", "Xinhua" = "dotted","China Daily" = "longdash","People\'s Daily" = "dotdash")) 


ggsave(paste0(path,'/Output/figure3b_softnews.png'),soft_p,width=7, height=5)
```

```{r Figure 3(c) Country Coverage}
country = fread(paste0(path,"/Data/Country Mapping/geo_analysis_output.csv"))%>%
  mutate(month=as.yearmon(month))%>%
    filter(month<as.yearmon("Jan 2020"))%>%
  filter(month>as.yearmon("Dec 2012"))

country_p<-country%>%
  filter(username %in% c("CGTN","China Daily","Xinhua","People\'s Daily"))%>%
  ggplot(aes(x=as.yearmon(month), y=country_count,linetype=username),size=0.5)+
 geom_line()+
  expand_limits(x=as.yearmon("Dec 2021"))+
   scale_x_yearmon(format = "%Y")+
  theme_bw()+
  geom_text_repel(aes(label=ifelse(month=="December 2019",as.character(username),'')),hjust=-1.2,vjust=0.1,direction= "y",segment.color = 'grey80')+
  labs(y="Number of Countries Mentioned per Month", x = "",title="")+ 
  theme(legend.position = "none",
        legend.title = element_blank(),
        legend.text=element_text(size=12), axis.text.x = element_text(size=12),
        axis.text.y = element_text(size=12), axis.title=element_text(size=12))+
  scale_linetype_manual(values = c("CGTN" = "solid", "Xinhua" = "dotted","China Daily" = "longdash","People\'s Daily" = "dotdash"))

ggsave(paste0(path,"/Output/figure3c_country_coverage.png"),country_p,width=7, height=5)

foreign_country_p<-country%>%
  filter(!username %in% c("CGTN","China Daily","Xinhua","People\'s Daily"))%>%
  ggplot(aes(x=as.yearmon(month), y=country_count,linetype=username),size=0.5)+
 geom_line()+
  expand_limits(x=as.yearmon("Dec 2021"))+
   scale_x_yearmon(format = "%Y")+
  theme_bw()+
  ylim(0,150)+
  geom_text_repel(aes(label=ifelse(month=="December 2019",as.character(username),'')),hjust=-1.2,vjust=0.1,direction= "y",segment.color = 'grey80')+
  labs(y="Number of Countries Mentioned per Month", x = "",title="")+ 
  theme(legend.position = "none",
        legend.title = element_blank(),
        legend.text=element_text(size=12), axis.text.x = element_text(size=12),
        axis.text.y = element_text(size=12), axis.title=element_text(size=12))+
  scale_linetype_manual(values = c("Al Jazeera" = "solid", "BBC Breaking" = "dotted","CNN Breaking" = "longdash","RT" = "dotdash"))
ggsave(paste0(path,'/Output/figureS6_country_coverage_all.png'),foreign_country_p,width=7, height=5)
```

```{r Figure 3(d) Sentiment of Tweets}
set.seed(123)
sentiment = fread(paste0(path,"/Data/Sentiment/sentiment_summary.csv"))%>%
  mutate(timestamp=as.Date(timestamp))%>%
  mutate(month=as.yearmon(timestamp))%>%
   select(username,month,compound) %>% 
    group_by(username,month) %>%
     summarise(data = list(smean.cl.boot(cur_data(), conf.int = .95, B = 1000, na.rm = TRUE))) %>%
  tidyr::unnest_wider(data)%>%
  mutate(D=ifelse(as.yearmon(month)>"2017-07-20",1,0))

sent_p<-sentiment%>%
  filter(username %in% c("CGTN","China Daily","Xinhua","People\'s Daily"))%>%
ggplot(aes(x=month, y=Mean,linetype=username))+
  geom_line(aes(group=username))+
  geom_ribbon(aes(x=month, y=Mean, ymax=Lower, ymin=Upper), 
              alpha=0.2)+
  ylim(-0.25,0.25)+
  theme_bw()+
     expand_limits(x=as.yearmon("Jul 2021"))+
   scale_x_yearmon(format = "%Y")+
  geom_text_repel(aes(label=ifelse(month=="December 2019",as.character(username),'')),hjust=-1.2,vjust=0.3,direction= "y",segment.color = 'grey80')+
  labs(y="Sentiment of Tweets", x = "",title="")+ 
  theme(legend.position = "none",
        legend.title = element_blank(),
        legend.text=element_text(size=12), axis.text.x = element_text(size=12),
        axis.text.y = element_text(size=12), axis.title=element_text(size=12))+
  scale_linetype_manual(values = c("CGTN" = "solid", "Xinhua" = "dotted","China Daily" = "longdash","People\'s Daily" = "dotdash")) +
  guides(linetype = FALSE)

ggsave(paste0(path,'/Output/figure3d_sentiment.png'),sent_p,width=7, height=5)

sent_all_p<-sentiment%>%
  filter(!username %in% c("China Daily","Xinhua","People\'s Daily"))%>% 
ggplot(aes(x=month, y=Mean,linetype=username))+
  geom_line(aes(group=username))+
  theme_bw()+
     expand_limits(x=as.yearmon("Jul 2021"))+
   scale_x_yearmon(format = "%Y")+
  geom_text_repel(aes(label=ifelse(month=="December 2019",as.character(username),'')),hjust=-1.2,vjust=0.3,direction= "y",segment.color = 'grey80')+
  labs(y="Sentiment of Tweets", x = "",title="")+ 
  theme(legend.position = "none",
        legend.title = element_blank(),
        legend.text=element_text(size=12), axis.text.x = element_text(size=12),
        axis.text.y = element_text(size=12), axis.title=element_text(size=12))+
  scale_linetype_manual(values = c("CGTN" = "solid", "RT" = "dashed","Al Jazeera" = "twodash","BBC Breaking" = "dotted","CNN Breaking" = "longdash")) +
  guides(scale = "none")
ggsave(paste0(path, "/Output/figureS7_sentiment_foreign.png"),sent_all_p,width=7, height=5)


```
